import $ from 'jquery';
import bus from '@/components/common/bus.js';
import httptool from './zry.plugins.httptool.js';
export default {
	cloneObj(obj) { //复制对象
		if(typeof obj == 'string') {
			obj = JSON.parse(obj)
		}
		if(obj instanceof Object) {
			var newObj = {}
			for(var k in obj) {
				newObj[k] = obj[k]
			}
		}
		return newObj;
	},
	changeHtmlTitle(title) {
		$('title').text(title);
	},
	getTargetDom(dom, targetClass) {
		var tag = 0
		while(!dom.hasClass(targetClass)) {
			tag++
			if(tag > 100) {
				return false;
			}
			dom = dom.parent()
			//console.log(dom)
		}
		return dom;
	},
	preview(ev, id = null) {
		console.log($(ev.target).attr('src'))
		if(id) {
			let src = httptool.rootUrl + 'view/' + id;

			let html = `<div class="imgPreview my_flex_center" >
							<div>
								<img src="${src}" alt="" />
							</div>
						</div>`
			$('body').append(html);

			$(".imgPreview").unbind().click(e => {
				console.log(e.target)
				let dom = $(e.target)
				let isPreview = $(e.target).hasClass('imgPreview')
				if(isPreview) {
					dom.remove()
				}
			})
		}

	},
	getImgUrlArr(urls, arr) { //根据拼接的url往图片墙数组中添加数据
		if(urls) {
			var strArr = urls.split(',');
			for(var i in strArr) {
				arr.push(strArr[i]);
			}
			console.log(arr);
		}
	},
	setStorage(key_, val) {
		if(typeof val == 'object') {
			val = JSON.stringify(val)
		}
		sessionStorage.setItem(key_, val)
	},
	getDirByKey(self, arr) {
		return new Promise((resolve, reject) => {
			let arr_key = [];

			for(let key_ of arr) {
				let obj = self.$store.state[key_];
				if(!obj) {
					obj = {};
				}
				if(Object.keys(obj).length == 0) {
					arr_key.push(key_);
				}
			}
			if(arr_key.length == 0) {
				resolve();
			} else {
				let params = {
					categorys: arr_key,
					pageNum: 1,
					pageSize: 100
				};
				self.httptool.ajaxPost("dict/getListValue", params, res => {
					res.arr_key = arr_key;
					bus.$emit('changeValue', res);
					resolve();
				})
			}
		})
	}
}